package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.alibaba.cola.dto.Response;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.TaskLinkStatusEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.TaskNodeTypeEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.TaskCompletedPostProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserTaskManageService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.BeanCopierUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.FlowUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.exception.EventCallSuspendException;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.eventconfig.EventExceptionDomain;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.monitor.FlowTaskTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.UserInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowEventExceptionRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskTraceRepository;
import lombok.extern.slf4j.Slf4j;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
　　* @description: 同步任务完成状态给待办组件
　　* @author shenrm
　　* @date 2021/12/28 16:27
　　*/
@Slf4j
@Service
@Order(2)
public class SyncTaskCompletedStatusProcessor implements TaskCompletedPostProcessor {

    @Autowired
    private UserTaskManageService userTaskManageService;

    @Autowired
    private UserService userService;

    @Autowired
    private FlowEventExceptionRepository flowEventExceptionRepository;

    @Autowired
    private FlowTaskTraceRepository flowTaskTraceRepository;


    @Override
    public void process(TaskEntity taskEntity) throws EventCallSuspendException {
        log.info("TaskCompletedStatusProcessor同步待办组件接口入参：{}",taskEntity);
        // 校验是否是会签节点,如果是会签节点,则是给每个人生成一条待办任务,每个人的taskId不一样,否则,就生成一条待办任务,所有的人拥有的taskId是一样的
        Boolean isMultiTask = FlowUtils.getInstance().isMultiTaskActivity(taskEntity.getProcessInstanceId(),taskEntity.getTaskDefinitionKey());
        List<String> taskInstanceIds = new ArrayList<>();
        taskInstanceIds.add(taskEntity.getId());
    }
   //使用注入的方式调用会产生循环注入    所以捕获异常的代码放在该处
    public void catchException(EventExceptionDomain temp, TaskEntity taskEntity){
        try {
            EventExceptionDomain eventExceptionDomain = new EventExceptionDomain();
            BeanCopierUtils.copyProperties(temp,eventExceptionDomain);
            eventExceptionDomain.setCallMethod("异步");
            eventExceptionDomain.setCallbackType("url");
            if (Objects.nonNull(taskEntity)) {
                String processInstanceId = taskEntity.getProcessInstanceId();
                eventExceptionDomain.setProcessInstanceId(processInstanceId);
                eventExceptionDomain.setNodeCode(taskEntity.getTaskDefinitionKey());  //获取节点编码
                eventExceptionDomain.setModelId(getModelIdFromProcessDefinition(taskEntity.getProcessDefinitionId()));
                // 根据taskId获取任务轨迹记录，填充modelName & modelKey
                FlowTaskTraceVo flowTaskTraceVo = flowTaskTraceRepository.getTaskTraceByTaskId(taskEntity.getId());
                if (Objects.nonNull(flowTaskTraceVo)) {
                    eventExceptionDomain.setModelName(flowTaskTraceVo.getModelName());
                    eventExceptionDomain.setModelKey(flowTaskTraceVo.getModelKey());
                    //eventExceptionDomain.setNodeCode(flowTaskTraceVo.getActivityId()); //获取节点编码
                }
            }
            flowEventExceptionRepository.insertEventException(eventExceptionDomain);
        }catch(Exception e){
            log.error("待办异常新增时出现异常，异常信息为：{}",e.getMessage(),e);
        }

    }
    public String getModelIdFromProcessDefinition(String processDefinition){
        if(StringUtils.isBlank(processDefinition)){
            return processDefinition;
        }
        if(processDefinition.contains(":")){
            String[] temp=processDefinition.split(":");
            if(temp.length==3){
                return temp[2];
            }
        }
        return processDefinition;
    }

    @Override
    public String getDesc() {
        return null;
    }
}
